package UserInterface;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import Main.Main;


interface AdditionalFunction {
	void run();
}

@SuppressWarnings("serial")
/**
 * General purpose screen object
 */
public class Screen extends JPanel implements MouseListener, MouseMotionListener
{
	
	private AdditionalFunction additionalFunction;
	private ImageIcon backgroundImage;
	private ArrayList<Link> links;
	/**
	 * Constructs the new screen by setting the background image, call-back functions and mouse listeners.
	 * @param image is the background image of the screen
	 * @param af is the additional function which will be executed after the initialization of the screen.
	 */
	public Screen(ImageIcon image,AdditionalFunction af)
	{
		additionalFunction = af;
		backgroundImage = image;
		
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}
	
	/**
	 * Renders the screen and its links
	 */
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Image image = backgroundImage.getImage();
		g.drawImage(image, 0, 0, this.getParent().getWidth(), this.getParent().getHeight(), null);
		if( links != null){
			for( int i = 0; i < links.size(); i++)
				if( links.get(i).isActive())
					g.drawRect(links.get(i).getX(), links.get(i).getY(), links.get(i).getWidth(), links.get(i).getHeight());
		}
		if( Main.GUI.getActiveScreen() == Main.GUI.getOptionsScreen()){
			if(!Main.mygame.getBoat().getColor())
				g.setColor( Color.RED);
			else
				g.setColor(Color.GREEN);
			g.fillRect(230, 226, 304, 10);
			g.setColor( Color.BLACK);
		}
		this.repaint();
	}
	
	/**
	 * Executes the additionl function if it is not null
	 */
	public void show(boolean implementFunction)
	{
		if (this.additionalFunction!=null)
		{
			if (implementFunction)
				additionalFunction.run();
		}
	}
	
	/**
	 * Renders the each link on the screen with the coordinates of x,y
	 * @param x is the x coordinate
	 * @param y is the y coordinate
	 */
	public void render(int x, int y)
	{
		for(int i = 0; i < this.links.size(); i++)
			this.links.get(i).render(x, y);
	}
	
	/**
	 * Executes the active link's call back function on the screen.
	 */
	public void selects()
	{
		for(int i =0;i<this.links.size();i++)
		{
			if (this.links.get(i).isActive())
				this.links.get(i).getCallback().run();
		}
	}

	
	//----------------------
	
	public AdditionalFunction getAdditionalFunction() {
		return additionalFunction;
	}

	public void setAdditionalFunction(AdditionalFunction additionalFunction) {
		this.additionalFunction = additionalFunction;
	}

	public ImageIcon getBackgroundImage() {
		return backgroundImage;
	}

	public void setBackgroundImage(ImageIcon backgroundImage) {
		this.backgroundImage = backgroundImage;
	}

	public ArrayList<Link> getLinks() {
		return links;
	}

	public void setLinks(ArrayList<Link> links) {
		this.links = links;
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {

	}
	
	public void mouseClicked(MouseEvent e) {
		if (this.links!= null){
		for( int i = 0; i < this.links.size(); i++)
			if( this.links.get(i).getX() < e.getX() && e.getX() < ( this.links.get(i).getX() + this.links.get(i).getWidth()) && this.links.get(i).getY() < e.getY() && e.getY() < ( this.links.get(i).getY() + this.links.get(i).getHeight()) )
				if( !this.links.get(i).isAlwaysActive())
					this.links.get(i).getCallback().run();
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		if (this.links!= null){
			for( int i = 0; i < this.links.size(); i++)
				if( this.links.get(i).getX() < e.getX() && e.getX() < ( this.links.get(i).getX() + this.links.get(i).getWidth()) && this.links.get(i).getY() < e.getY() && e.getY() < ( this.links.get(i).getY() + this.links.get(i).getHeight()) )
					this.links.get(i).setActive(true);
				else
					if(!this.links.get(i).isAlwaysActive())
						this.links.get(i).setActive(false);
		}
		
	}

	
}